import { ref, unref, computed, watch } from 'vue'
import {
  designUtils,
  getCustomComDesignInfo,
  useComDesignStoreWithOut,
} from '@/store'

import {
  getNodeComSlots,
  BwComTypeEnum,
  BwCustomComSoltTypeEnum,
  BwCustomComNodeIfFromTypeEnum as BwIfFromTypeEnum,
  BwCustomComNodeForFromTypeEnum as BwForFromTypeEnum,
  type BwCustomComStructModel,
  type BwCustomComNodeIfInfoModel,
  type BwCustomComNodeForInfoModel,
  type BwCustomComNodeEnableSlotInfoModel,
} from '@bitwit/base-com-render'

export function useNodeSeniorSetup() {
  const designStore = useComDesignStoreWithOut()

  const curNodeId = ref<string>('')
  const nodeSlotExpand = ref<boolean>(true)
  const nodeIfInofExpand = ref<boolean>(true)
  const nodeForInofExpand = ref<boolean>(true)
  const curNodeIfInfo = ref<BwCustomComNodeIfInfoModel>({})
  const curNodeForInfo = ref<BwCustomComNodeForInfoModel>({})
  const curNodeSlotList = ref<Array<BwCustomComNodeEnableSlotInfoModel>>([])

  const bwValueFromTypeList = computed(() => {
    const list = [
      { value: BwIfFromTypeEnum.constValue, label: '常量' },
      { value: BwIfFromTypeEnum.comProps, label: '外放属性' },
      { value: BwIfFromTypeEnum.comVar, label: '组件变量' },
      { value: BwIfFromTypeEnum.JSExpression, label: 'js表达式' },
    ]
    return list
  })

  /** 修改节点 If 值 */
  const onChangeNodeIfValue = (value: any) => {
    const cusIfInfo = unref(curNodeIfInfo)
    const valueFrom = cusIfInfo.conditionVarFrom

    switch (valueFrom) {
      case BwIfFromTypeEnum.JSExpression: {
        cusIfInfo.jsExpParams = value.jsExpParams
        cusIfInfo.jsExpBodyLogic = value.jsExpBodyLogic
        break
      }
      case BwIfFromTypeEnum.comProps: {
        cusIfInfo.comPropsName = value
        break
      }
      case BwIfFromTypeEnum.comVar: {
        cusIfInfo.comVarName = value
        break
      }
      case BwIfFromTypeEnum.constValue: {
        cusIfInfo.constValue = value
        break
      }
    }

    designStore.onChangeNodeEleIfInfo(unref(curNodeId), cusIfInfo)
  }

  /** 修改节点 If 值来源 */
  const onChangeNodeIfValueFrom = (data: BwIfFromTypeEnum) => {
    const cusInfo = unref(curNodeIfInfo)
    cusInfo.conditionVarFrom = data
    designStore.onChangeNodeEleIfInfo(unref(curNodeId), cusInfo)
  }

  /** 修改节点 For 值  */
  const onChangeNodeForValue = (value: any) => {
    const cusInfo = unref(curNodeForInfo)
    const valueFrom = cusInfo.forVarFrom

    switch (valueFrom) {
      case BwForFromTypeEnum.JSExpression: {
        cusInfo.jsExpParams = value.jsExpParams
        cusInfo.jsExpBodyLogic = value.jsExpBodyLogic
        break
      }
      case BwForFromTypeEnum.comProps: {
        cusInfo.comPropsName = value
        break
      }
      case BwForFromTypeEnum.comVar: {
        cusInfo.comVarName = value
        break
      }
      case BwForFromTypeEnum.constValue: {
        cusInfo.constValue = value
        break
      }
    }

    designStore.onChangeNodeEleForInfo(unref(curNodeId), cusInfo)
  }

  /** 修改节点 For 值来源 */
  const onChangeNodeForValueFrom = (data: BwForFromTypeEnum) => {
    const cusInfo = unref(curNodeForInfo)
    cusInfo.forVarFrom = data
    designStore.onChangeNodeEleForInfo(unref(curNodeId), cusInfo)
  }

  /** 获取插槽名称 */
  const getSlotName = (data: BwCustomComNodeEnableSlotInfoModel) => {
    let name = ''
    switch (data.slotType) {
      case BwCustomComSoltTypeEnum.trends: {
        name = data.trendsSlotInfo?.logicName ?? ''
        break
      }
      case BwCustomComSoltTypeEnum.convention: {
        name = data.conventionSlotInfo?.slotName ?? ''
        break
      }
    }
    return name
  }

  /** 获取插槽标识 */
  const getSlotCode = (data: BwCustomComNodeEnableSlotInfoModel) => {
    let code = ''
    switch (data.slotType) {
      case BwCustomComSoltTypeEnum.trends: {
        code = data.trendsSlotInfo?.logicCode ?? ''
        break
      }
      case BwCustomComSoltTypeEnum.convention: {
        code = data.conventionSlotInfo?.slotCode ?? ''
        break
      }
    }
    return code
  }

  /** 获取插槽标识 */
  const getSlotType = (data: BwCustomComNodeEnableSlotInfoModel) => {
    let text = ''
    switch (data.slotType) {
      case BwCustomComSoltTypeEnum.trends: {
        text = '动态插槽'
        break
      }
      case BwCustomComSoltTypeEnum.convention: {
        text = '常规插槽'
        break
      }
    }
    return text
  }

  /** 设置插槽是否启用 */
  const onSlotVisible = (
    data: BwCustomComNodeEnableSlotInfoModel,
    visible: boolean
  ) => {
    if (data) {
      data.visible = visible
      designStore.onChangeEnableNodeSlot(unref(curNodeId), data, visible)
    }
  }

  /** 初始化插槽列表 */
  const initNodeComSlotList = (nodeInfo: BwCustomComStructModel) => {
    const list: Array<BwCustomComNodeEnableSlotInfoModel> = []
    const enableList = nodeInfo.enableSlotList ?? []
    switch (nodeInfo.comType) {
      case BwComTypeEnum.custom: {
        getCustomComDesignInfo(
          nodeInfo.comAppCode ?? '',
          nodeInfo.comName
        ).then((res) => {
          const slotList = res?.comSlots ?? []

          if (Array.isArray(slotList)) {
            slotList.forEach((item) => {
              const curSlotInfo = designUtils.findComSlotInfo(enableList, item)
              if (curSlotInfo.info) {
                list.push(curSlotInfo.info)
              } else {
                list.push({
                  ...item,
                  visible: false,
                })
              }
            })
          }
          curNodeSlotList.value = list
        })
        break
      }
      default: {
        const slotList = getNodeComSlots(nodeInfo.comName)
        if (Array.isArray(slotList)) {
          slotList.forEach((item) => {
            const curSlotInfo = designUtils.findComSlotInfo(enableList, item)
            if (curSlotInfo.info) {
              list.push(curSlotInfo.info)
            } else {
              list.push({ ...item, visible: false })
            }
          })
        }
        curNodeSlotList.value = list
      }
    }
  }

  watch(
    () => designStore.selectNodeEle,
    (newVal) => {
      if (newVal) {
        curNodeId.value = newVal.nodeId
        curNodeIfInfo.value = newVal.nodeIfInfo ?? {}
        curNodeForInfo.value = newVal.nodeForInfo ?? {}

        initNodeComSlotList(newVal)
      } else {
        curNodeId.value = ''
        curNodeIfInfo.value = {}
        curNodeForInfo.value = {}
        curNodeSlotList.value = []
      }
    },
    { deep: true, immediate: true }
  )

  return {
    curNodeIfInfo,
    curNodeForInfo,
    curNodeSlotList,
    nodeSlotExpand,
    nodeIfInofExpand,
    nodeForInofExpand,
    bwValueFromTypeList,

    getSlotType,
    getSlotCode,
    getSlotName,
    onSlotVisible,
    onChangeNodeIfValue,
    onChangeNodeIfValueFrom,
    onChangeNodeForValue,
    onChangeNodeForValueFrom,
  }
}
